package com.ruoyi.web.controller.vantController.customerController;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.AliyunSmsUtils;
import com.ruoyi.system.domain.customer.CustomerAddr;
import com.ruoyi.system.domain.customer.CustomerCoupon;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.supe.PlatformCoupon;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.PreferentialCard;
import com.ruoyi.system.service.vantservice.customerervice.ApiCustomerService;
import com.ruoyi.system.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 客户信息前端API接口控制层
 */
@Api(value = "前端客户接口", tags = {"前端客户接口"})
@RestController
@RequestMapping("/api/customer/info")
@CrossOrigin
public class ApiCustomerController extends BaseController {

    @Autowired
    private ApiCustomerService customService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TokenUtils tokenUtils;

    @Value("${ruoyi.telphone}")
    private String telPhone;


    @ApiOperation(value = "查询用户地址列表")
    @GetMapping("/selectCustomerAddressList")
    public AjaxResult selectCustomerAddressList() {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        return AjaxResult.success(customService.selectCustomerAddressList(customerLogin.getCustomerId()));
    }

    @ApiOperation(value = "根据地址ID查询用户地址列表")
    @GetMapping("/selectCustomerAddressById/{addressId}")
    public AjaxResult selectCustomerAddressById(@PathVariable(value = "addressId", required = false)Long addressId) {
        return AjaxResult.success(customService.selectCustomerAddrById(addressId));
    }

    @ApiOperation(value = "新增地址信息")
    @PostMapping("/insertCustomerAddr")
    public AjaxResult insertCustomerAddr(@Validated @RequestBody CustomerAddr customerAddr) {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        customerAddr.setCustomerId(customerLogin.getCustomerId());
        return AjaxResult.success(customService.insertCustomerAddr(customerAddr));
    }

    @ApiOperation(value = "修改地址信息")
    @PutMapping("/updateCustomerAddr")
    public AjaxResult updateCustomerAddr(@Validated @RequestBody CustomerAddr customerAddr) {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        return AjaxResult.success(customService.updateCustomerAddr(customerAddr,customerLogin.getCustomerId()));
    }

    @ApiOperation(value = "切换默认地址信息")
    @GetMapping("/updateCustomerAddrDefault/{addressId}")
    public AjaxResult updateCustomerAddrDefault(@PathVariable(value = "addressId", required = false)Long addressId) {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        return AjaxResult.success(customService.updateCustomerAddrDefault(addressId,customerLogin.getCustomerId()));
    }


    @ApiOperation(value = "删除地址信息")
    @DeleteMapping("/deleteCustomerAddrById/{addressId}")
    public AjaxResult deleteCustomerAddrById(@PathVariable(value = "addressId", required = false)Long addressId) {
        return AjaxResult.success(customService.deleteCustomerAddrById(addressId));
    }

    // TODO:这个接口是订单用的,查询客户的地址信息
    @ApiOperation(value = "根据客户ID查询地址默认信息")
    @GetMapping("/selectCustomerAddrByCustomerId")
    public AjaxResult selectCustomerAddrByCustomerId() {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        if(customService.selectCustomerAddrByCustomerId(customerLogin.getCustomerId()) !=null){
            return AjaxResult.success(customService.selectCustomerAddrByCustomerId(customerLogin.getCustomerId()));
        }else{
            return AjaxResult.error("无默认地址");
        }
    }

    @ApiOperation(value = "查询客户可用优惠券")
    @GetMapping("/selectCustomerCouponList/{supplierId}")
    public AjaxResult selectCustomerCouponList(@PathVariable(value = "supplierId", required = false)Integer supplierId){
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        return AjaxResult.success(customService.selectCustomerCouponList(customerLogin.getCustomerId().intValue(),supplierId));
    }

    @ApiOperation(value = "我的:查询客户可用优惠券")
    @GetMapping("/selectCustomerCoupon")
    public AjaxResult selectCustomerCoupon(){
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        return AjaxResult.success(customService.selectCustomerCoupon(customerLogin.getCustomerId()));
    }


    @ApiOperation(value = "领取店铺优惠券")
    @PostMapping("/installCustomerCode")
    public AjaxResult installCustomerCode(@RequestBody PreferentialCard preferentialCard){
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        return toAjax(customService.installCustomerCode(preferentialCard,customerLogin.getCustomerId()));
    }

    @ApiOperation(value = "领取平台优惠券")
    @PostMapping("/addPlatformCouponCustomerCode")
    public AjaxResult addPlatformCouponCustomerCode(@RequestBody PlatformCoupon platformCoupon){
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        return toAjax(customService.addPlatformCouponCustomerCode(platformCoupon,customerLogin.getCustomerId()));
    }

    @ApiOperation(value = "设置优惠券状态为已使用")
    @GetMapping("/updateCustomerCouponState/{couponId}")
    public AjaxResult updateCustomerCouponState(@PathVariable(value = "couponId", required = false)Long couponId ){
        int count = customService.updateCustomerCouponState(couponId);
        if(count>0){
            // 创建短信验证码类对象
            AliyunSmsUtils smsUtils = new AliyunSmsUtils();
            try {
                //发送短信
                SendSmsResponse response = smsUtils.sendSms(telPhone, null);
            } catch (ClientException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return toAjax(count);
    }
}
